
from my import *
from .utils import *

toolname = getToolName()

from time import *
import sys,math,threading,os

def showMsg(msg,title):
    os.system("mshta vbscript:msgbox(\"{0}\",64,\"{1}\")(window.close)".format(msg,title))
    


class Alarm(threading.Thread):
    def __init__(self, matter, targtTime):
        threading.Thread.__init__(self)
        self.matter = matter
        self.targtTime = targtTime
    def run(self):
        pass


def func(isHelp=False):
    if(isHelp):return [toolname,"~ %s m h s -"%(toolname),"#alarm"]
    argv = getToolArgv()
    if(len(sys.argv)<2):print("?");return
    m=int(sys.argv[2]) if(len(sys.argv)>=3) else 0
    h=int(sys.argv[3]) if(len(sys.argv)>=4) else 0
    s=int(sys.argv[4]) if(len(sys.argv)>=5) else 0
    matter=sys.argv[5] if(len(sys.argv)>=6) else "alarm"
    startTime=time()
    targtTime=time()+h*60*60+m*60+s
    try:
        while(time()<targtTime):
            sleep(0.1)
            if(time()<targtTime):
                restTime=int((targtTime - time())*10)/10
            else: restTime=int((time() - targtTime)*10)/10
            h=int(restTime/(60*60) %24)
            m=int(restTime/60 %60)
            s=restTime%60
            print("{:>3}:{:>3}:{:>5.3} {}".format(h,m,float(s),"" if(time()<targtTime)else matter),end="\r")
        if(matter != 'nop'):
            for i in range(10):
                showMsg(matter, "Alarm "+str(int(time() - targtTime)))
                sleep(60)
    except:
        pass